package com.szxy.backend.content.controller;

import com.szxy.backend.content.service.ContentCategoryService;
import com.szxy.backend.content.service.ContentService;
import com.szxy.pojo.TbContent;
import com.szxy.pojo.TbContentCategory;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther:zwer
 * @Date:2019/9/9 20:56
 * @Description:com.szxy.content.controller
 * @Version:1.0
 **/
@RestController
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private ContentCategoryService contentCategoryService;

    /**
     * 分类内容管理 查询
     *
     * @param id
     * @return
     */
    @PostMapping("/selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id) {
        try {
            return this.contentCategoryService.selectContentCategoryByParentId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500, "error");
    }

    /**
     * 内容分类管理 添加接口
     *
     * @param contentCategory
     * @return
     */
    @PostMapping("/insertContentCategory")
    public Result insertContentCategory(TbContentCategory contentCategory) {
        try {
            return this.contentCategoryService.insertContentCategory(contentCategory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500, "error");
    }

    /**
     * 删除节点信息
     * @param categoryId
     * @return
     */
    @PostMapping("/deleteContentCategoryById")
    public Result deleteContentCategoryById(Long categoryId) {
        try {
            return this.contentCategoryService.deleteContentCategoryById(categoryId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500, "error");
    }

    /**
     * 更新节点信息
     * @param contentCategory
     * @return
     */
    @PostMapping("/updateContentCategory")
    public Result updateContentCategory(TbContentCategory contentCategory) {
        try {
            return this.contentCategoryService.updateContentCategory(contentCategory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500, "error");
    }

    /**
     * 查找内容节点信息
     * @param categoryId
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("/selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(Long categoryId,
                                                 @RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10")Integer rows) {
        try {
            return this.contentService.selectTbContentAllByCategoryId(categoryId,page,rows);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.build(500, "error");
    }

    /**
     * 插入内容信息
     * @param content
     * @return
     */
    @PostMapping("/insertTbContent")
    Result insertTbContent(TbContent content) {
        try {
            return this.contentService.insertTbContent(content);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

    /**
     * 删除内容信息
     * @param ids 内容 ID
     * @return
     */
    @PostMapping("/deleteContentByIds")
    Result deleteContentByIds(Long ids){
        try {
            return this.contentService.deleteContentByIds(ids);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.build(500,"error");
    }

}
